package lvcy.message.sender.builder.impl;

import java.lang.reflect.Constructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lvcy.message.entity.RequestEntity;
import lvcy.message.sender.DxtMessageSender;
import lvcy.message.sender.MessageSender;
import lvcy.message.sender.builder.DxtMessageSenderBuilder;

public class DxtSingleMessageSenderBuilder extends DxtMessageSenderBuilder{

	private Logger log=LogManager.getLogger(this);
	@Override
	public MessageSender build(RequestEntity request) {
		
		String postData=getPostData(request);
		try {
			Class<?> clazz=Class.forName(DxtMessageSender.class.getName());
			Constructor<?> constructor=clazz.getDeclaredConstructor(String.class,String.class);
			sender=(DxtMessageSender)constructor.newInstance("",postData);
		} catch (Exception e) {
			log.error(e);
		}
		return sender;
	}
	@Override
	protected String getPostData(RequestEntity request){
		
		StringBuilder builder=new StringBuilder(super.getPostData(request));
		if(request.getPhones().size()!=1){
			throw new IllegalArgumentException("The phones' size must be one");
		}
		builder.append("Phones=").append(request.getPhones().get(0));
		return builder.toString();
	}

	
}
